package com.zwd.deep.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwd.deep.common.IntegerEnum;
import com.zwd.deep.entity.dto.DiagnosisDto;
import com.zwd.deep.entity.po.Task;
import com.zwd.deep.entity.vo.TaskVo;
import com.zwd.deep.mapper.TaskMapper;
import com.zwd.deep.service.DeepService;
import com.zwd.deep.service.TaskService;
import com.zwd.deep.utils.AliOSSUtils;
import com.zwd.deep.utils.HttpServletUtil;
import com.zwd.deep.utils.JwtUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 任务表 服务实现类
 *
 * @author zwd
 * @since 2024-11-13
 */
@Service
@AllArgsConstructor
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {

    private final DeepService deepService;
    private final TaskMapper taskMapper;
    private final AliOSSUtils aliOSSUtils;

    @Override
    public String createTask(MultipartFile file) {
        String result = deepService.getTaskId(file);
        if (Objects.isNull(result)) {
            throw new RuntimeException("创建任务失败");
        }
        Task task = new Task();
//        task.setUserId(Long.valueOf(JwtUtil.extractId(HttpServletUtil.getToken())));
        String path = aliOSSUtils.upload(file);
        task.setTaskImageUrl(path);
        task.setTaskId(result);
        int insert = taskMapper.insert(task);
        if (insert <= IntegerEnum.ZERO) {
            throw new RuntimeException("插入失败");
        }
        return result;
    }

    @Override
    public List<TaskVo> getHistory() {
        List<Task> tasks = taskMapper.selectHistory(JwtUtil.extractId(HttpServletUtil.getToken()));
        List<TaskVo> taskVoList = new ArrayList<>();
        for (Task task : tasks) {
            TaskVo taskVo = new TaskVo();
            BeanUtil.copyProperties(task, taskVo);
            taskVoList.add(taskVo);
        }
        return taskVoList;
    }

    @Override
    public String diagnose(DiagnosisDto aiDiagnoseDto) {
        return null;
    }

    @Override
    public String check(MultipartFile file) {
        String result = deepService.getResult(file);
        return result;
    }
}
